home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / ibmscrn.c < prev    next >
C/C++ Source or Header  |  1996-07-23  |  10KB  |  496 lines

  1. /*
  2.  *  IBMSCRN.C
  3.  *
  4.  *  Written on 10-Jul-94 by John Dennis and released to the public domain.
  5.  *
  6.  *  Screen definitions and routines for the IBM PC only; EGA/VGA support 
  7.  *  not using a commercial screen product.
  8.  *
  9.  *  Uses an event queue for mouse and keyboard input.  Currently doesn't ask
  10.  *  ask for a DESQview buffer and doesn't give up any ticks - this should be
  11.  *  implemented in some compiler independant fashion.
  12.  */
  13.  
  14. #include "msged.h"
  15. #include "winsys.h"
  16. #include "vio.h"
  17. #include "unused.h"
  18. #include "dosasm.h"
  19.  
  20. #define EBUFSZ 100
  21.  
  22. #define TERMDEF  1
  23.  
  24. /* #define NO_MOUSE 1 */
  25.  
  26. #ifdef  MSDOS
  27.  
  28. #include <time.h>
  29.  
  30. #ifndef PACIFIC
  31. #include <sys/timeb.h>
  32. #include <bios.h>
  33. #endif
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #define NCOL 80
  39. #define NROW 25
  40.  
  41. #include <stdio.h>
  42. #include <dos.h>
  43.  
  44. /* ===[ variables ]=== */
  45.  
  46. TERM term =
  47. {
  48.     NCOL,
  49.     NROW,
  50.     0,
  51. };
  52.  
  53. unsigned int color;
  54. int LButton = 0;
  55. int RButton = 0;
  56. int cur_start = 6;              /* these are ega/vga defaults */
  57. int cur_end = 7;
  58. static EVT EVent[EBUFSZ];       /* event circular queue */
  59. static int ebufin = 0;          /* event in */
  60. static int ebufout = 0;         /* event out */
  61. static MOU mstatus =
  62. {0, 40, 12, 0, 0, 1, 1, 0};     /* mouse status */
  63. static int mouse_avail = 0;
  64. static int mousex = 40;
  65. static int mousey = 12;
  66.  
  67. static int mykbhit(void);
  68. static int FullBuffer(void);
  69.  
  70. /* ====[ functions ]==== */
  71.  
  72. int TTScolor(unsigned int Attr)
  73. {
  74.     VIOsetfore(Attr & 0x000f);
  75.     VIOsetback((Attr & 0xfff0) / 0x10);
  76.     color = Attr;
  77.     return 0;
  78. }
  79.  
  80. int TTBeep(void)
  81. {
  82.     return 0;
  83. }
  84.  
  85. int TTCurSet(int st)
  86. {
  87.     union REGS regs;
  88.  
  89.     if (st)
  90.     {                           /* Restores cursor               */
  91.         regs.h.ah = 0x01;       /* BIOS Set Cursor Type          */
  92.         regs.h.ch = cur_start;  /* Retrieve starting scan line   */
  93.         regs.h.cl = cur_end;    /* Retrieve ending scan line     */
  94. #if defined(MSDOS) && defined(__FLAT__)
  95.         int386(0x10, ®s, ®s);
  96. #else
  97.         int86(0x10, ®s, ®s);  /* Call BIOS                     */
  98. #endif
  99.     }
  100.     else
  101.     {                           /* Removes cursor                */
  102.         regs.h.ah = 0x01;       /* BIOS Set Cursor Type          */
  103.         regs.h.ch = 0x20;       /* Set bit 5 in CH               */
  104. #if defined(MSDOS) && defined(__FLAT__)
  105.         int386(0x10, ®s, ®s);
  106. #else
  107.         int86(0x10, ®s, ®s);  /* Call BIOS                     */
  108. #endif
  109.     }
  110.     return 0;
  111. }
  112.  
  113. int TTgotoxy(int row, int col)
  114. {
  115.     VIOgotoxy(col, row);
  116.     VIOupdate();
  117.     return 0;
  118. }
  119.  
  120. int TTgetxy(int *row, int *col)
  121. {
  122.     *row = VIOwherex();
  123.     *col = VIOwherey();
  124.     return 0;
  125. }
  126.  
  127. unsigned int TTGetKey(void)
  128. {
  129.     return (obtkey());
  130. }
  131.  
  132. int TTPutChr(unsigned int Ch)
  133. {
  134.     VIOputc(Ch);
  135.     VIOupdate();
  136.     return 0;
  137. }
  138.  
  139. int TTWriteStr(unsigned short *b, int len, int row, int col)
  140. {
  141.     VIOputr(col, row, len, 1, b);
  142.     return 0;
  143. }
  144.  
  145. int TTStrWr(unsigned char *s, int row, int col)
  146. {
  147.     unsigned short line[200];
  148.     int i = 0;
  149.  
  150.     while (*s)
  151.     {
  152.         line[i] = ((unsigned)*s & 0xff) | (color << 8);
  153.         s++;
  154.         i++;
  155.     }
  156.     TTWriteStr(line, i, row, col);
  157.     return 0;
  158. }
  159.  
  160. int TTReadStr(unsigned short *b, int len, int row, int col)
  161. {
  162.     VIOgetra(col, row, col + len - 1, row, b);
  163.     return 0;
  164. }
  165.  
  166. int TTClear(int x1, int y1, int x2, int y2)
  167. {
  168.     VIOclear(x1, y1, x2, y2);
  169.     return 0;
  170. }
  171.  
  172. int TTScroll(int x1, int y1, int x2, int y2, int lines, int Dir)
  173. {
  174.     if (Dir)
  175.         VIOscrollup(x1, y1, x2 + 1, y2, lines);
  176.     else
  177.         VIOscrolldown(x1, y1, x2 + 1, y2, lines);
  178.     return 0;
  179. }
  180.  
  181. int TTEeol(void)
  182. {
  183.     int row, col;
  184.  
  185.     TTgetxy(&row, &col);
  186.     TTScroll(col, row, NCOL - 1, row, 0, 1);
  187.     return 0;
  188. }
  189.  
  190. int TTChngRez(int a)
  191. {
  192.     unused(a);
  193.     return 0;
  194. }
  195.  
  196. int TTdelay(int mil)
  197. {
  198. #ifdef __TURBOC__
  199.     delay(mil);
  200. #else
  201.     unused(mil);
  202. #endif
  203.     return 0;
  204. }
  205.  
  206. void TTSendMsg(int msg, int x, int y, int msgtype)
  207. {
  208.     if (((ebufin + 1) % EBUFSZ) != ebufout)
  209.     {
  210.         EVent[ebufin].msg = msg;
  211.         EVent[ebufin].x = x;
  212.         EVent[ebufin].y = y;
  213.         EVent[ebufin].msgtype = msgtype;
  214.         ebufin = (ebufin + 1) % EBUFSZ;
  215.     }
  216. }
  217.  
  218. /* ======================================== */
  219.  
  220. /*
  221.    **
  222.    ** This is primarily the input section of the file.
  223.    ** All messages are handled here...
  224.    **
  225.  */
  226.  
  227. #define CLICKMAX                 50  /* second max */
  228. #define REPEAT_PAUSE             800
  229. #define T_DOS                    1
  230. #define T_DV                     2
  231. #define T_WINDOWS                3
  232.  
  233. int ms_reset(int *mousetype);
  234. int ms_show_cursor(void);
  235. int ms_hide_cursor(void);
  236. int ms_get_mouse_pos(int *horizpos, int *vertpos);
  237.  
  238. static unsigned long rtimer = -1;
  239. static unsigned long ltimer = -1;
  240. static int mtask = T_DOS;
  241.  
  242. void pause(void)
  243. {
  244.     switch (mtask)
  245.     {
  246.     case T_DOS:
  247.         dospause();
  248.         break;
  249.  
  250.     case T_DV:
  251.         dvpause();
  252.         break;
  253.  
  254.     case T_WINDOWS:
  255.         winpause();
  256.         break;
  257.     default:
  258.         break;
  259.     }
  260. }
  261.  
  262. static unsigned long hsec_time(void)
  263. {
  264.     static unsigned long old_id = 0;
  265.     unsigned long i;
  266. #if defined(__TURBOC__)
  267.     struct time t;
  268.  
  269.     gettime(&t);
  270.  
  271.     i = (t.ti_sec * 100L) + t.ti_hund;
  272. #elif defined(PACIFIC)
  273.     i = 0;
  274. #else
  275.     struct dostime_t dtime;
  276.  
  277.     _dos_gettime(&dtime);
  278.     i = (dtime.second * 100L) + dtime.hsecond;
  279. #endif
  280.  
  281.     return (old_id = i);
  282. }
  283.  
  284. /* we should give up some ticks here... typically when polling this routine */
  285. /* it is quite possible to loop through this in less than a millisecond...  */
  286. /* Obviuosly this resolution is not needed :-)                              */
  287.  
  288. int collect_events(void)
  289. {
  290.     static int oy = 0, ox = 0;
  291.     int moved = 0;
  292.     int evt = 0;
  293.     int ret;
  294.  
  295.     if (mouse_avail)
  296.     {
  297.         ret = ms_get_mouse_pos(&mousex, &mousey);
  298.  
  299.         if (mousex != ox || mousey != oy)
  300.         {
  301.             ox = mstatus.x = mousex;
  302.             oy = mstatus.y = mousey;
  303.             moved = 1;
  304.         }
  305.         if (mstatus.lrelease && (ret & 1))
  306.         {
  307.             evt = 1;
  308.             mstatus.lbutton = 1;
  309.             mstatus.lrelease = 0;
  310.             ltimer = hsec_time() + CLICKMAX;
  311.             TTSendMsg(MOU_LBTDN, ox, oy, WM_MOUSE);
  312.         }
  313.         if (mstatus.lbutton && !(ret & 1))
  314.         {
  315.             evt = 1;
  316.             mstatus.lbutton = 0;
  317.             mstatus.lrelease = 1;
  318.             TTSendMsg(MOU_LBTUP, ox, oy, WM_MOUSE);
  319.             if (hsec_time() < ltimer)
  320.                 TTSendMsg(LMOU_CLCK, ox, oy, WM_MOUSE);
  321.         }
  322.         if (mstatus.rrelease && (ret & 2))
  323.         {
  324.             evt = 1;
  325.             mstatus.rbutton = 1;
  326.             mstatus.rrelease = 0;
  327.             rtimer = hsec_time() + CLICKMAX;
  328.             TTSendMsg(MOU_RBTDN, ox, oy, WM_MOUSE);
  329.         }
  330.         if (mstatus.rbutton && !(ret & 2))
  331.         {
  332.             evt = 1;
  333.             mstatus.rbutton = 0;
  334.             mstatus.rrelease = 1;
  335.             TTSendMsg(MOU_RBTUP, ox, oy, WM_MOUSE);
  336.             if (hsec_time() < rtimer)
  337.                 TTSendMsg(RMOU_CLCK, ox, oy, WM_MOUSE);
  338.         }
  339.         if (!evt)
  340.         {
  341.             if (mstatus.lbutton || mstatus.rbutton)
  342.             {
  343.                 if (mstatus.lbutton && hsec_time() > ltimer)
  344.                     TTSendMsg(LMOU_RPT, ox, oy, WM_MOUSE);
  345.  
  346.                 if (mstatus.rbutton && hsec_time() > rtimer)
  347.                     TTSendMsg(RMOU_RPT, ox, oy, WM_MOUSE);
  348.  
  349.                 if (moved)
  350.                     TTSendMsg(MOUSE_EVT, ox, oy, WM_MOUSE);
  351.             }
  352.         }
  353.         if (mykbhit())
  354.             TTSendMsg(TTGetKey(), 0, 0, WM_CHAR);
  355.     }
  356.     else if (mykbhit())
  357.         TTSendMsg(TTGetKey(), 0, 0, WM_CHAR);
  358.  
  359.     return 0;
  360. }
  361.  
  362. int TTGetMsg(EVT * event)
  363. {
  364.     while (ebufin == ebufout)
  365.     {
  366.         pause();
  367.         collect_events();
  368.     }
  369.  
  370.     event->msg = EVent[ebufout].msg;
  371.     event->msgtype = E